package src.bean;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import src.utils.resource.ResourcesUtils;

import java.io.IOException;
import java.util.Properties;

public class HbaseConn {
//    public static final String FAMILY_NAME = "f1";
//    public static final String FAMILY_NAME_F2 = "f2";
//
//    public static final byte[] CF = Bytes.toBytes(FAMILY_NAME);
//    public static final byte[] CF_2 = Bytes.toBytes(FAMILY_NAME_F2);

    private static Connection connection = null;

    private static volatile HbaseConn instance;

    private HbaseConn() {
        try {
            Configuration conf = HBaseConfiguration.create();
            Properties pro = ResourcesUtils.getResourceAsProperties("HbaseConfig.properties");
//            String ip = pro.getProperty("ip");
//            System.out.println(ip);
            conf.set("hbase.zookeeper.quorum", pro.getProperty("ip"));
            conf.set("hbase.zookeeper.property.clientPort", pro.getProperty("port"));
            connection = ConnectionFactory.createConnection(conf);
            System.out.println("创建connection！");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static Connection getConnection() {
        return connection;
    }

    public static HbaseConn getIstance() {
        // 定义一个共有的静态方法，返回该类型实例
        if (instance == null) {
            // 对象实例化时与否判断（不使用同步代码块，instance不等于null时，直接返回对象，提高运行效率）
            synchronized (HbaseConn.class) {
                // 同步代码块（对象未初始化时，使用同步代码块，保证多线程访问时对象在第一次创建后，不再重复被创建）
                if (instance == null) {
                    // 未初始化，则初始instance变量
                    instance = new HbaseConn();
                }
            }
        }
        return instance;
    }
}


